﻿Imports System.Xml.Serialization

Namespace Commands

    Public Class GetShares
        Inherits Framework.Net.CommandBase

        Private Const mRequestCode As String = "GetSharesRequest"
        Private Const mReplyCode As String = "GetSharesReply"

        Public Sub New()
            MyBase.New(mRequestCode, mReplyCode)
        End Sub

        Public Class Request
        End Class
        Public Class Reply
            Public Groups As New Generic.List(Of Group)
        End Class
        Public Class Group
            <XmlAttribute()> Public Name As String
            Public Shares As New Generic.List(Of Entities.Infos.ShareInfo)
            Public Filters As New Generic.List(Of Filter)
            Public Sub New()
            End Sub
            Public Sub New(ByVal name As String)
                Me.Name = name
            End Sub
        End Class
        Public Class Filter
            <XmlAttribute()> Public Code As String
            <XmlText()> Public Name As String
            Public Sub New()
            End Sub
            Public Sub New(ByVal code As String, ByVal name As String)
                Me.Code = code : Me.Name = name
            End Sub
        End Class

        Public Shared Function SendRequest(ByVal node As Framework.Net.Node, ByVal callback As ReplyDelegate, ByVal invokeCallbackOnMainGuiThread As Boolean) As Integer
            Dim clsRequest As New Request
            Dim iMessageId As Integer = Framework.Net.Manager.GetNextMessageId()
            RegisterCallback(iMessageId, callback, invokeCallbackOnMainGuiThread)
            node.SendCommandMessageAsync(iMessageId, mRequestCode, Framework.Serialization.ToXmlBytes(clsRequest))
            Return iMessageId
        End Function
        Public Delegate Sub ReplyDelegate(ByVal inReplyToMessageId As Integer, ByVal node As Framework.Net.Node, ByVal reply As Reply)
        Public Overrides Sub HandleReply(ByVal envelope As Framework.Net.MessageEnvelope)
            'Dim clsReply As Reply = Framework.Serialization.FromXmlBytes(Of Reply)(envelope.Message.Message)
            Dim clsReply As Reply = Framework.Serialization.FromXmlBytes(Of Reply)(envelope.Message.Message)
            InvokeCallback(envelope.Message.InReplyToId, envelope.Node, clsReply)
        End Sub

        Public Overrides Sub HandleRequest(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim aShares As Generic.List(Of Entities.Share)
            aShares = EntityHandlers.Shares.GetAll(envelope.Node.BuddyKeyMaster)

            Dim clsReply As New Reply

            Dim clsGroup As New Group("Shares")
            clsReply.Groups.Add(clsGroup)
            clsGroup.Shares = EntityHandlers.Shares.GetAllAsInfos()

            clsGroup = New Group("Filters")
            clsReply.Groups.Add(clsGroup)
            clsGroup.Filters.Add(New Filter("\RecentAdditions", "Recent additions"))

            envelope.Node.SendReplyCommandMessageAsync(envelope, mReplyCode, Framework.Serialization.ToXmlBytes(clsReply))
        End Sub
    End Class


End Namespace
